import { _decorator, Component, instantiate, math, Prefab } from "cc";
const { ccclass, property } = _decorator;

@ccclass("EnemyManeger")
export class EnemyManeger extends Component {
  // 1号敌机
  @property
  enemy0SpawnRate: number = 0.8;
  @property(Prefab)
  enemy0Prefab: Prefab = null;
  // 2号敌机
  @property
  enemy1SpawnRate: number = 3;
  @property(Prefab)
  enemy1Prefab: Prefab = null;
  // 3号敌机
  @property
  enemy2SpawnRate: number = 10;
  @property(Prefab)
  enemy2Prefab: Prefab = null;
  // 奖励物品
  @property
  rewardSpawnRate: number = 15;
  @property(Prefab)
  rewardPrefab1: Prefab = null;
  @property(Prefab)
  rewardPrefab2: Prefab = null;

  start() {
    this.schedule(this.onEnemy0Spawn, this.enemy0SpawnRate);
    this.schedule(this.onEnemy1Spawn, this.enemy1SpawnRate);
    this.schedule(this.onEnemy2Spawn, this.enemy2SpawnRate);
    this.schedule(this.onRewardSpawn, this.enemy2SpawnRate);
  }
  protected onDestroy(): void {
    this.unschedule(this.onEnemy0Spawn);
    this.unschedule(this.onEnemy1Spawn);
    this.unschedule(this.onEnemy2Spawn);
  }

  update(deltaTime: number) {}
  onEnemy0Spawn() {
    // -215 / 215
    this.prefadSpawnHandle(this.enemy0Prefab, -215, 215, 450);
  }
  onEnemy1Spawn() {
    // -215 / 215
    this.prefadSpawnHandle(this.enemy1Prefab, -200, 200, 475);
  }
  onEnemy2Spawn() {
    // -215 / 215
    this.prefadSpawnHandle(this.enemy2Prefab, -155, 155, 560);
  }
  onRewardSpawn() {
    const random = math.randomRangeInt(0, 2);
    const rewardPrefab = random === 0 ? this.rewardPrefab1 : this.rewardPrefab2;
    this.prefadSpawnHandle(rewardPrefab, -190, 190, 470);
  }
  prefadSpawnHandle(
    enemyPrefad: Prefab,
    minX: number,
    maxX: number,
    posY: number
  ) {
    const enemy = instantiate(enemyPrefad);
    this.node.addChild(enemy);
    const randomX = math.randomRangeInt(minX, maxX);
    enemy.setPosition(randomX, posY, 0);
  }
}
